package com.zgzg.shop.pay.base.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 日期时间处理
 *
 * @author zzl
 * @date 2021-08-11
 */
public class DateTimeUtils {

    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_MIN_PATTERN = "yyyy-MM-dd HH:mm";

    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss:SSS";

    public static final String DATE_TIME_REGEXP_MESSAGE = "时间解析格式(区间于2000-01-01 00:00:00 ~ 2099-12-31 23:59:59): " + DATE_TIME_PATTERN;

    public static String format(LocalDateTime localDateTime) {
        return format(localDateTime, DATE_TIME_PATTERN);
    }

    public static String format(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDateTime parse(String dateTime) {
        return parse(dateTime, DATE_TIME_PATTERN);
    }


    public static LocalDateTime parse(String dateTime, String pattern) {
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(pattern);
        try {
            return LocalDateTime.parse(dateTime, ofPattern);
        } catch (DateTimeParseException e) {
            return LocalDate.parse(dateTime, ofPattern).atStartOfDay();
        }
    }

    public static boolean before(LocalDateTime localDateTime) {
        return LocalDateTime.now().isBefore(localDateTime);
    }

    public static boolean after(LocalDateTime localDateTime) {
        return LocalDateTime.now().isAfter(localDateTime);
    }

    /**
     * 将毫秒转换为时分秒格式
     *
     * @param millisecond 毫秒数
     * @return HH:mm:ss
     */
    public static String formatDuring(long millisecond) {
        long hours = (millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (millisecond % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (millisecond % (1000 * 60)) / 1000;

        String zero = "0", colon = ":";
        return (hours < 10 ? zero + hours : hours) + colon
                + (minutes < 10 ? zero + minutes : minutes) + colon
                + (seconds < 10 ? zero + seconds : seconds);
    }

    /**
     * 获取过期时间
     * @author qy
     * @param expiresIn: 过期秒数
     * @return 当前时间+过期秒数
     **/
    public static String getExpiresTime(final int expiresIn) {
        // 当前时间加上过期时间的秒数
        Date date = DateUtils.addSeconds(new Date(), expiresIn);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return format(localDateTime);
    }

    /*
     *  计算两个时间的间隔，计算会向下取整。
     *  例如：ChronoUnit.SECONDS   2022-08-25T15:34:28.405.until(2022-08-25T15:35:06) = 37秒  而不是 38秒
     **/
    public static long timeInterval(LocalDateTime beginLocalDateTime, LocalDateTime endLocalDateTime, ChronoUnit chronoUnit) {
        if (beginLocalDateTime != null && endLocalDateTime != null) {
            return Math.abs(endLocalDateTime.until(beginLocalDateTime, chronoUnit));
        }
        return 0L;
    }

    public static LocalDateTime addDateTime(LocalDateTime localDateTime, int amount){
        if(localDateTime == null){
            return null;
        }
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); //需要说明时区
        Date from = Date.from(instant);
        return LocalDateTime.ofInstant(DateUtils.addDays(from, amount).toInstant(), ZoneId.systemDefault());
    }


}
